const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const express = require('express');
const bodyParser = require('body-parser');
const url = require('url');
const proxy = require('express-http-proxy');
// const { req } = require('./utils/util');

const router = express.Router();

module.exports = (app) => {
  if (!app) {
    app = require('express')();
  }

  const mapi = 'https://api.dev.pajkdc.com/m.api';
  
  app.use(bodyParser.json());

  // https://www.jianshu.com/p/846e8b555ead
  app.use('/m.api', proxy(mapi), {
  	forwardPath: function(req, res) {
      console.log(chalk.green(`[req]${req.url}`));
  		return url.parse(req.url).path;
  	 }
  });

  app.use('/static', express.static(path.join(__dirname, '../src/static')));

  app.listen(8080, () => console.log('Open URL:', 'http://localhost:8080/'));

  const actions = {
    ['healthIot.parseData']: async (req, res, next) => {

    }
  };

  // router.all('/m.api', (req, res, next) => {
  //   const { _mt } = req.query;
  //   console.log(_mt + ':', req.query);

  //   let result = { code: 404, msg: 'not found' };
  //   if (actions[_mt]) {
  //     result = actions[_mt](req, res, next);
  //     res.json(result);
  //   } else {
  //     res.sendStatus(404);
  //   }
  // });
  // app.use(router);

};
